package com.linkage.bookshop.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linkage.bookshop.model.Book;
import com.linkage.bookshop.model.Trolleyline;
import com.linkage.bookshop.repository.UserRepository;
import com.linkage.bookshop.service.inter.BookService;
import com.linkage.bookshop.service.inter.TrolleyService;
@Service("trolleyService")
public class TrolleyServiceImpl implements TrolleyService {
	@Autowired
	private BookService bookService;
	@Autowired
	UserRepository userRepository;

	// 加入购物车
	@Override
	public Cookie joinTrolley(Cookie[] cookies, String username, int bookId,
			int amount) {
		int userId=userRepository.selectUserId(username);
		Cookie cookie = null;
		// 找到对应用户的cookie,没有则新建一个
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(String.valueOf(userId))) {
				cookie = cookies[i];
				break;
			} else {
				cookie = new Cookie(String.valueOf(userId), "");
			}
		}
		String oldString = cookie.getValue();
		cookie.setValue(oldString  + bookId + "," + amount+ ",");
		return cookie;
	}

	// 从购物车移除
	@Override
	public Cookie removeFromTrolley(Cookie[] cookies, String username,
			int bookId, int amount) {
		int userId=userRepository.selectUserId(username);
		Cookie cookie = null;
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(String.valueOf(userId))) {
				cookie = cookies[i];
				break;
			}
		}
		String string = cookie.getValue();
		String[] str = string.split(",");
		int oldAmount = 0;
		int n = 0;
		// 从cookie里找到对应bookId的数量
		for (int i = 0; i < str.length; i++) {
			if (String.valueOf(bookId).equals(str[i])) {
				n = i;
				oldAmount = Integer.parseInt(str[i + 1]);
				break;
			}
		}
		// 购物车里的数量比削减的数量少就返回原来的
		if (oldAmount < amount) {
			return cookie;
		}
		String s1 = "";
		String s2 = "";
		// 购物车里的数量和削减的一样就移除此种书
		if (oldAmount == amount) {
			if (n == (str.length - 2)) {
				if(n==0){
					cookie.setMaxAge(0);
				}else{
				for (int i = 0; i < n - 1; i++) {
					s1 = s1 + str[i] + ",";
				}
				string = s1 + str[n - 1];
				cookie.setValue(string);
				}
				return cookie;
			}
			for (int i = 0; i < n; i++) {
				s1 = s1 + str[i] + ",";
			}
			for (int i = n + 2; i < (str.length - 1); i++) {
				s2 = s2 + str[i] + ",";
			}
			string = s1 + s2 + str[str.length - 1];
			cookie.setValue(string);
			return cookie;
		} else {
			str[n + 1] = String.valueOf(oldAmount - amount);
			for (int i = 0; i < (str.length - 1); i++) {
				s1 = s1 + str[i] + ",";
			}
			string = s1 + str[str.length - 1];
			cookie.setValue(string);
			return cookie;
		}
	}

	// 查看购物车
	@Override
	public List<Trolleyline> checkTrolley(Cookie[] cookies, String username) {
		int userId=userRepository.selectUserId(username);
		Cookie cookie=null;
		boolean b=true;
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(String.valueOf(userId))) {
				cookie = cookies[i];
				break;
			} else {
				cookie = new Cookie(String.valueOf(userId), "");
				b=false;
			}
			
		}
		if(!b){
			return null;
		}
		Book book = new Book();
		String string = cookie.getValue();
		String[] str = string.split(",");
		List<Trolleyline> tls = new ArrayList<Trolleyline>();
		for (int i = 0; i < str.length;) {
			Trolleyline tl = new Trolleyline();
			book = bookService.loadBookDetail(Integer.parseInt(str[i]));
			tl.setAmount(Integer.parseInt(str[i + 1]));
			tl.setBook(book);
			double bookPrice = (book.getOutPrice() * Integer
					.parseInt(str[i + 1]));
			tl.setBookPrice(bookPrice);
			tls.add(tl);
			i = i + 2;
		}
		return tls;
	}

	// 验证削减数量是否合法
	@Override
	public boolean checkAmount(Cookie[] cookies, String username, int bookId,
			int amount) {
		int userId=userRepository.selectUserId(username);
		Cookie cookie = null;
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(String.valueOf(userId))) {
				cookie = cookies[i];
				break;
			}
		}
		String string = cookie.getValue();
		String[] str = string.split(",");
		int oldAmount = 0;
		for (int i = 0; i < str.length; i++) {
			if (String.valueOf(bookId).equals(str[i])) {
				oldAmount = Integer.parseInt(str[i + 1]);
				break;
			}
		}
		if (oldAmount < amount) {
			return false;
		}
		return true;
		
		
	}
	/*移除cookie*/
	public Cookie removeCookie(String username,Cookie[] cookies){
		int userId=userRepository.selectUserId(username);
		Cookie cookie = null;
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(String.valueOf(userId))) {
				cookie = cookies[i];
				break;
			}
		}
		cookie.setMaxAge(0);
		return cookie;
	}

}
